//
//  AppCache.m
//  MyProject
//
//  Created by yun on 2018/4/26.
//  Copyright © 2018年 yun. All rights reserved.
//

#import "AppCache.h"

@implementation AppCache

+ (NSUInteger)getSize{
    
    NSUInteger size = 0;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    
    NSDirectoryEnumerator *fileEnumerator = [fileManager enumeratorAtPath:cachePath];
    
    for (NSString *fileName in fileEnumerator) {
        NSString *filePath = [cachePath stringByAppendingPathComponent:fileName];
        NSDictionary<NSString *, id> *attrs = [fileManager attributesOfItemAtPath:filePath error:nil];
        size += [attrs fileSize];
    }
    return size;
}

+ (void)clearDiskCache{
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    NSArray *fileDirs = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:cachePath error:nil];// 获取子文件夹
    for (NSString *subPath in fileDirs) {
        [fileManager removeItemAtPath:[cachePath stringByAppendingPathComponent:subPath] error:nil];
    }
}


@end
